/**
 * Created by Administrator on 2015/11/24.
 */

var IUserService = require('../api/service').IUserService
var utils = require('utility')
var helper = require('./helper')
var User = require('../domain/domain').User

module.exports = function (app) {

    // ---用户主页---
    app.get("/user/:nick", function *() {
        var $self = this
        var nick = $self.params.nick
        console.log("nick is ", nick)
        if(!nick){
            $self.status = 404
            return
        }
        //var user = yield IUserService.getUserByNick(nick)
        //user = JSON.parse(user)
        //if(!user || !user.id){
        //    yield $self.render("user/nobody", {
        //        title: "用户不存在"
        //    })
        //}
        //if($self.session.user && $self.session.user.id == user.id){
        //
        //}

        var user = {}
        user.nick = nick
        user.id = 1

        var recipes = yield $self.db('SELECT * FROM Recipe WHERE owner_id=?', user.id)

        yield this.render("user/home", {
            title: nick + "的主页",
            user: user,
            recipes: recipes
        })
    })

    // ----用户登录----
    app.post('/login', function *() {
        var $self = this
        var body = $self.request.body
        if(!body.username || !body.password){
            $self.status = 404
            return
        }
        var user = yield $self.db('SELECT * FROM User WHERE username=? AND password=?', [body.username, body.password])
        if(!user){
            $self.status = 404
            return
        }
        $self.session.user = user
        $self.response.redirect('/')
    })

    app.get('/login/test', function *() {
        var $self = this
        var user = (yield $self.db('SELECT * FROM User WHERE id=?', 1))[0]
        $self.session.user = user
        $self.response.redirect("/")
    })

    // ---注册---
    app.post('/signin', function *() {
        var $self = this
        var body = $self.request.body
        var user = new User(body)
        user.username = "13824843156"
        user.nick = "thanatos"
        user.password = utils.md5("123456", "base64")
        if(!user.username || !user.password || !user.nick){
            yield $self.body = {
                code: 1,
                message: "数据填写不完全"
            }
            return
        }
        //yield IUserService.signin(user.username, user.nick, user.password)
        yield $self.db('INSERT INTO User SET ?', user)
        $self.session.user = user
        yield $self.body = {
            code: 0,
            message: "注册成功"
        }
    })

    // ---设置个人资料页面---
    app.get('/user/update/info', function *() {
        var $self = this
        var user = $self.session.user
        if(!user || !user.id){
            $self.status = 404
            return
        }
        user = $self.db('SELECT * FROM User WHERE id=?', user.id)
        yield $self.render("user/update_info", {
            user: user
        })
    })

    // ---设置个人资料----
    app.post('/user/update/info', function *() {
        var $self = this
        var body = $self.request.body
        var user = helper.remain(body, "nick", "gender", "telephone", "qq", "birthday", "address")
        yield $self.db('UPDATE User SET ? WHERE id=?', [user, $self.session.user.id])
        $self.response.redirect('/user/update/info')
    })


}